package com.hawthorn.redis.service.dept.impl;

import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.entity.dto.SysDeptDTO;
import com.hawthorn.entity.po.SysDeptPO;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.redis.repository.DeptCacheMapper;
import com.hawthorn.redis.service.dept.DeptCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>todo 加载部门缓存服务实现类 </p>
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 2021/11/26 12:32 PM
 * @version v1.0.2
 * @copyright: Copyright (c)  andyten
 */
@Service
@Slf4j
public class DeptCacheServiceImpl implements DeptCacheService
{
  @Resource
  RedisMyClient redisMyClient;

  @Resource
  private DeptCacheMapper deptCacheMapper;

  @Transactional(readOnly = true)
  public void init()
  {
    List<SysDeptDTO> deptCache = deptCacheMapper.selectDept();
    String key;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DEPT_PREFIX, "*"));

    for (SysDeptDTO deptCacheDTO : deptCache)
    {
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DEPT_PREFIX, deptCacheDTO.getId());

      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DCODE, deptCacheDTO.getDcode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DNAME, deptCacheDTO.getDname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_SNAME, deptCacheDTO.getSname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_ENAME, deptCacheDTO.getEname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DC, deptCacheDTO.getDcode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_PID, MyUtil.getValue(deptCacheDTO.getPid()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_LEVEL, MyUtil.getValue(deptCacheDTO.getLevel()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_LANGUAG, MyUtil.getValue(deptCacheDTO.getLanguage()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DEPTKIND, MyUtil.getValue(deptCacheDTO.getDeptKind()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DEPTTYPE, MyUtil.getValue(deptCacheDTO.getDeptType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_BIZSTATUS, MyUtil.getValue(deptCacheDTO.getBizStatus()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_BIZTYPE, MyUtil.getValue(deptCacheDTO.getBizType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_COMPANYID, MyUtil.getValue(deptCacheDTO.getCompanyId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_OEMCOMPANYID, MyUtil.getValue(deptCacheDTO.getOemCompanyId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_ERPCODE, MyUtil.getValue(deptCacheDTO.getErpCode()));
    }

    log.info("====== load deptdata to redis success ======");
  }

  @Override
  public void updateDept(SysDeptPO sysDeptPO)
  {
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DEPT_PREFIX, sysDeptPO.getId());

    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DCODE, sysDeptPO.getDcode());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DNAME, sysDeptPO.getDname());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_SNAME, sysDeptPO.getSname());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_ENAME, sysDeptPO.getEname());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DC, sysDeptPO.getDcode());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_PID, MyUtil.getValue(sysDeptPO.getPid()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_LEVEL, MyUtil.getValue(sysDeptPO.getLevel()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_LANGUAG, MyUtil.getValue(sysDeptPO.getLanguage()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DEPTKIND, MyUtil.getValue(sysDeptPO.getDeptKind()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DEPTTYPE, MyUtil.getValue(sysDeptPO.getDeptType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_BIZSTATUS, MyUtil.getValue(sysDeptPO.getBizStatus()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_BIZTYPE, MyUtil.getValue(sysDeptPO.getBizType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_COMPANYID, MyUtil.getValue(sysDeptPO.getCompanyId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_OEMCOMPANYID, MyUtil.getValue(sysDeptPO.getOemCompanyId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_ERPCODE, MyUtil.getValue(sysDeptPO.getErpCode()));

    log.info("====== load dept data to redis successfully ======");
  }

  @Override
  public void updateDeptById(Long deptId)
  {
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DEPT_PREFIX, deptId);
    SysDeptPO sysDeptPO = deptCacheMapper.selectById(deptId);
    if (sysDeptPO != null)
    {
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DCODE, sysDeptPO.getDcode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DNAME, sysDeptPO.getDname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_SNAME, sysDeptPO.getSname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_ENAME, sysDeptPO.getEname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DC, sysDeptPO.getDcode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_PID, MyUtil.getValue(sysDeptPO.getPid()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_LEVEL, MyUtil.getValue(sysDeptPO.getLevel()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_LANGUAG, MyUtil.getValue(sysDeptPO.getLanguage()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DEPTKIND, MyUtil.getValue(sysDeptPO.getDeptKind()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_DEPTTYPE, MyUtil.getValue(sysDeptPO.getDeptType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_BIZSTATUS, MyUtil.getValue(sysDeptPO.getBizStatus()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_BIZTYPE, MyUtil.getValue(sysDeptPO.getBizType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_COMPANYID, MyUtil.getValue(sysDeptPO.getCompanyId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_OEMCOMPANYID, MyUtil.getValue(sysDeptPO.getOemCompanyId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DEPT_ERPCODE, MyUtil.getValue(sysDeptPO.getErpCode()));

      log.info("====== load dept data to redis successfully [" + deptId + "]======");
    }

  }
}
